package com.bamboo.pistorage.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.crypto.SecureUtil;
import com.bamboo.core.enums.code.CodeCommonEnum;
import com.bamboo.core.exceptions.BambooException;
import com.bamboo.model.domain.StorageResultEntity;
import com.bamboo.model.enums.CloudVendorEnum;
import com.bamboo.model.mapper.StorageResultMapper;
import com.bamboo.pistorage.service.IStorageResultService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.List;

/**
* @description 
* @date 2025/3/27 下午3:13
* @author chenzh
*/
@Service
public class StorageResultServiceImpl extends ServiceImpl<StorageResultMapper, StorageResultEntity>
        implements IStorageResultService {

    @Override
    public Long saveUploadResult(StorageResultEntity rowEntity) {
        if (this.save(rowEntity)) {
            return rowEntity.getId();
        }
        throw new BambooException(CodeCommonEnum.DATA_CREATE_FAILED);
    }

    @Override
    public StorageResultEntity fetchUploadResult(Long id) {
        StorageResultEntity resultEntity = this.getById(id);
        if (resultEntity == null) {
            throw new BambooException(CodeCommonEnum.EMPTY_DATA);
        }
        return resultEntity;
    }

    @Override
    public StorageResultEntity fetchUploadResult(String objKey, CloudVendorEnum cloudVendor) {
//        LambdaQueryWrapper<StorageResultEntity> queryWrapper = Wrappers.lambdaQuery(StorageResultEntity.class)
//                .eq(StorageResultEntity::getObjKey, objKey)
//                .eq(StorageResultEntity::getCloudVendorEnum, cloudVendor).last("LIMIT 1");
        QueryWrapper<StorageResultEntity> queryWrapper = Wrappers.query(StorageResultEntity.class)
                .eq("obj_key", objKey)
                .eq("cloud_vendor", cloudVendor.getCloud()).last("LIMIT 1");
        StorageResultEntity resultEntity = this.getOne(queryWrapper);
        if (resultEntity == null) {
            throw new BambooException(CodeCommonEnum.EMPTY_DATA);
        }
        return resultEntity;
    }

    @Override
    public StorageResultEntity hasUploaded(CloudVendorEnum cloudVendor, File localFile) {
//        LambdaQueryWrapper<StorageResultEntity> queryWrapper = Wrappers.lambdaQuery(StorageResultEntity.class)
//                .eq(StorageResultEntity::getCloudVendorEnum, cloudVendor)
//                .eq(StorageResultEntity::getFileMd5, SecureUtil.md5(localFile));
        QueryWrapper<StorageResultEntity> queryWrapper = Wrappers.query(StorageResultEntity.class)
                .eq("cloud_vendor", cloudVendor.getCloud())
                .eq("file_md5", SecureUtil.md5(localFile));
        List<StorageResultEntity> list = this.list(queryWrapper);
        if (CollUtil.isEmpty(list)) {
            return null;
        }
        return list.get(0);
    }

    @Override
    public boolean deleteUploadResult(Long id) {
        return this.removeById(id);
    }
}
